package org.supsi.skarabee.model;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
;

/**
 * @author Rea
 *
 */
public class NodeModel extends Observable{

	private static final NodeModel instance = new NodeModel();

	private JSONObject node;	

	/**
	 * private constructor
	 */
	private NodeModel(){
	}

	/**
	 * @return the model singleton instance
	 */
	public static NodeModel getInstance(){
		return instance;
	}

	/**
	 * @param s
	 * Set the JSON data 
	 */
	public void setJson(String s){
		try {
			node = new JSONObject(s).getJSONObject("node");
			setChanged();	
			notifyObservers();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Bundle getNodeInfos(){
		Bundle b = new Bundle();
		b.putString("id", getNodeId());
		b.putString("config", getConfiguration());
		b.putString("status", getStatus());
		return b;
	}

	public Bundle getChartInfos(){
		Bundle b = new Bundle();
		b.putLong("ts", new Date().getTime());
		for(int i = 1; i <5; i++){
			b.putString(i + "", getPortVoltage(i));
			b.putString((i + 4) + "", getPortCurrent(i));
		}
		return b;
	}
	
	public Bundle portInfos(){
		Bundle b = new Bundle();
		
		return b;
	}

	/**
	 * @return the configuration of the node [0-15]
	 */
	private String getConfiguration() {
		String c = "";
		try {
			c = node.getString("config");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return c;
	}

	/**
	 * @return
	 */
	private String getStatus(){
		String s = "";
		try {
			s = node.getString("status");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return s;
	}

	/**
	 * @return
	 */
	private String getNodeId(){
		String id = "";
		try {
			id = node.getString("id");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return id;
	}

	/**
	 * @param port
	 * @return
	 */
	private String getPortVoltage(int port){
		String v = "";
		try {
			v = node.getJSONArray("voltage").getString(port-1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return v;
	}

	/**
	 * @param port
	 * @return
	 */
	private String getPortCurrent(int port){
		String c = "";
		try {
			c = node.getJSONArray("ampere").getString(port-1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}

	/**
	 * @param port
	 * @return
	 */
	private String getPortId(int port){
		String pId = "";
		try {
			pId = node.getJSONArray("pId").getString(port-1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  pId;
	}

	/**
	 * @param port
	 * @return
	 */
	private String getPortDevice(int port){
		String d = "";
		try {
			d = node.getJSONArray("device").getString(port-1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @param port
	 * @return
	 */
	private boolean isPortConnected(int port){
		boolean b = false;
		try {
			String s = node.getJSONArray("connected").getString(port-1);
			if(s.toUpperCase().equals("TRUE")){
				b = true;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

	/**
	 * @param port
	 * @return
	 */
	private String getAnnotation(int port){
		String a = "";
		try {
			a = node.getJSONArray("annotation").getString(port-1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}


	/* (non-Javadoc)
	 * @see java.util.Observable#addObserver(java.util.Observer)
	 */
	@Override
	public void addObserver(Observer observer) {
		// TODO Auto-generated method stub
		super.addObserver(observer);
	}


	/* (non-Javadoc)
	 * @see java.util.Observable#deleteObserver(java.util.Observer)
	 */
	@Override
	public synchronized void deleteObserver(Observer observer) {
		// TODO Auto-generated method stub
		super.deleteObserver(observer);
	}


	/* (non-Javadoc)
	 * @see java.util.Observable#notifyObservers()
	 */
	@Override

	public void notifyObservers() {
		// TODO Auto-generated method stub
		super.notifyObservers();
	}



}
